<template>
  <!--表情气泡-->
  <div ref="popup" class="emotion-popup clear-fix" @mouseleave="onMouseLeave">
    <!--表情-->
    <div
      v-for="(item, sIndex) in 104"
      :key="item"
      class="emoticon-item"
      :style="{ backgroundPosition: `-${sIndex * 24}px 0` }"
      @mouseover="hover($event, sIndex)"
      @click="$emit('item-click', sIndex)"></div>
    <!--gif 预览-->
    <div v-show="showPreview" class="preview-gif" :style="style">
      <img :src="`./static/img/emotion/${index}.gif`" alt="" />
    </div>
  </div>
</template>

<script>
  export default {
    name: 'Emotion',
    data() {
      return {
        showPreview: false,
        index: 0,
        style: {
          right: '5px'
        }
      }
    },
    methods: {
      onItemClick(item) {
        this.$notify({ title: '点击的位置', type: 'info', message: `${item}` })
      },
      hover(event, index) {
        this.showPreview = true
        this.index = index
        if (event.clientX - this.$refs.popup.getBoundingClientRect().left - 5 - 1 <= 196) {
          this.style = {
            right: '5px'
          }
        } else {
          this.style = {
            left: '5px'
          }
        }
      },
      onMouseLeave() {
        this.showPreview = false
        this.$emit('mouseleave')
      }
    }
  }
</script>

<style lang="scss" scoped>
  .emotion-popup {
    position: relative;
    width: 380px;
    min-width: 110px;
    margin: 20px 10px 10px;
    padding: 5px;
    background-color: #fff;
    border: 1px solid #c5d7ef;
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.09);
  }

  .emoticon-item {
    display: inline;
    float: left;
    box-sizing: content-box;
    width: 24px;
    height: 24px;
    margin: 0 2px 2px 0;
    line-height: 1.3;
    background: url('~@/assets/images/emotion.gif') no-repeat;
    border: 1px solid #fff;
    cursor: pointer;
  }

  .emoticon-item:hover,
  .preview-gif {
    border: 1px solid #5296d3;
  }

  .preview-gif {
    position: absolute;
    top: 5px;
    width: 60px;
    height: 60px;
    background-color: #fff;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
  }

  .preview-gif img {
    margin-top: 18px;
    margin-left: 18px;
  }
</style>
